<template>
  <el-dialog
    :visible.sync="visible"
    title="销项内容"
    append-to-body
    width="600px"
    @close="handleClose"
  >
    <div class="container">
      <el-input ref="inputRef" v-model="templateText" type="textarea" :autosize="{ minRows: 2, maxRows: 6}" />
      <div class="tag-block">
        <template v-for="(tagItem,index) in tagListComputed">
          <span :key="index" class="tag-item" @click="()=> { handleTagSelect(tagItem) }">{{ tagItem }}</span>
        </template>
      </div>
      <div class="temp-block">
        <div class="title">
          可选模板
        </div>
        <template v-for="(temp,index) in taskList">
          <div :key="index" class="temp-item" @click="() => { handleTempChange(temp.name) }">
            {{ temp.name }}
          </div>
        </template>
      </div>
    </div>

    <div slot="footer" class="footer">
      <el-button size="mini" @click="handleClose">
        取消
      </el-button>
      <el-button size="mini" type="primary" @click="handleOk">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { fetchDropTaskTemplate } from '@/api/commonDropDown'
import { isValid } from '@/utils'

const tagList = ['日期', '异常的下级指标']
export default {
  name: 'SelectTreeViewInput',
  data() {
    return {
      visible: false,
      taskList: [],
      tagList, //
      templateText: ''
    }
  },
  computed: {
    tagListComputed: function() {
      const { businessKpiParams } = this.$store.state.businessFormData.basicForm.data
      return [...tagList, ...businessKpiParams.map(e => e.name)]
    }
  },

  methods: {
    open(val) {
      this.$data.templateText = val || ''
      this.$data.visible = true
      this.postTaskTemplateList()
      return new Promise((resolve, reject) => {
        this.monitor = { resolve, reject }
      })
    },
    postTaskTemplateList() {
      fetchDropTaskTemplate().then(res => {
        this.$data.taskList = res
      })
    },
    handleTempChange(e) {
      this.$data.templateText = e
    },
    handleTagSelect(e) {
      const elInput = this.$refs.inputRef.$el.querySelector('textarea')
      const { templateText } = this.$data
      const textStart = templateText.substring(0, elInput.selectionStart)
      const textEnd = templateText.substring(elInput.selectionStart, templateText.length)

      this.$data.templateText = textStart + ' @' + e + '@ ' + textEnd
      elInput.focus()
    },

    handleClose() {
      this.$data.visible = false
      this.$emit('close')
    },
    handleOk() {
      if (!isValid(this.$data.templateText)) {
        this.$message.warning('内容不能为空')
        return
      }
      this.monitor.resolve(this.$data.templateText)
      this.handleClose()
    }

  }
}
</script>

<style lang="scss" scoped>
  .select{
    color: red;
  }
  .tag-block{
    margin-top: 20px;
    .tag-item{
      background-color: #f8f8f8;
      border: 1px solid #d9ddee;
      margin-right: 10px;
      margin-bottom: 4px;
      padding: 1px 5px;
      border-radius: 3px;
      font-size: 12px;
      cursor: pointer;
    }
  }
  .temp-block {
    margin-top: 20px;

    .title {
      margin-bottom: 10px;
      color: #666;
    }
    .temp-item {
      background-color: #fafbff;
      border: 1px solid #d9ddee;
      margin-bottom: 4px;
      padding: 5px 5px;
      border-radius: 3px;
      font-size: 12px;
      cursor: pointer;

    }
  }

</style>
